/*
 * Copyright (c) 2015. Visionet and/or its affiliates. All right reserved.
 * VISIONET PROPRIETARY/CONFIDENTIAL.
 */
package com.visionet.jumper.core.exception.runtime;

import com.visionet.jumper.core.support.spring.SpringMessageUtils;
import org.springframework.util.StringUtils;

/**
 * @author Sin
 * @date 12/2/2015.
 */
public class BaseRuntimeException extends RuntimeException {

	private static final long serialVersionUID = 8490219605526655161L;
	
	private String defaultMessage;

	public BaseRuntimeException(String defaultMessage, Throwable cause) {
		super(cause);
		this.defaultMessage = defaultMessage;
	}
	
	public BaseRuntimeException(String defaultMessage) {
		this(defaultMessage, null);
	}

	public String getDefaultMessage() {
		return defaultMessage;
	}

	public void setMessage(String defaultMessage) {
		this.defaultMessage = defaultMessage;
	}
	
	/**
	 * 获取自定义错误代码国际化消息，若无对应的国际化消息，则默认返回参数消息defaultMessage
	 * @return 国际化消息
	 */
	@Override
	public String getMessage() {
		String message = null;
        try {
			if (!StringUtils.isEmpty(defaultMessage)) {
			    message = SpringMessageUtils.message(defaultMessage);
			}
		} catch (Exception e) {
			message = defaultMessage;
		}
        if (message == null) {
            message = defaultMessage;
        }
        return message;
	}
}
